/*
 * @Author: szx
 * @Date: 2021-10-31 12:34:38
 * @LastEditTime: 2021-10-31 13:09:47
 * @Description:
 * @FilePath: \leetcode\1-99\3\3.cpp
 */
#include <iostream>
using namespace std;
class Solution {
   public:
    int lengthOfLongestSubstring(string s) {
        int n = s.length();
        if (n == 0) {
            return 0;
        }
        int max = 1;
        int sum = 1;
        string str(1, s[0]);
        for (int i = 1; i < n; i++) {
            auto p = str.find(s[i]);
            if (p == str.npos) {
                str += s[i];
                sum++;
            } else {
                str = str.substr(p + 1, str.size() - p + 1) + s[i];
                if (sum > max) {
                    max = sum;
                }
                sum = str.length();
            }
        }
        return sum > max ? sum : max;
    }
};

int main() {
    Solution s;
    cout << s.lengthOfLongestSubstring("abcd") << endl;
    return 0;
}